package com.ruoyi.common.utils;

import com.ruoyi.common.core.domain.hospital.MrInData;
import com.ruoyi.common.core.domain.hospital.MrInLab;
import com.ruoyi.common.core.domain.hospital.MrInLabs;
import com.ruoyi.common.core.domain.hospital.MrInLabsData;
import org.springframework.beans.factory.annotation.Autowired;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析MrInLabsData.xml 工具类
 *  实验室检查报告数据集
 */
public class MrInLabUtils {
    public static List<MrInLab> getMrInLab(String file) throws JAXBException {
        List<MrInLab> mrInLabList = new ArrayList<>();
        try {

            JAXBContext jaxbContext = JAXBContext.newInstance(MrInLabsData.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            StringReader reader = new StringReader(file);
            MrInLabsData mrInLabsData = (MrInLabsData) jaxbUnmarshaller.unmarshal(reader);
            mrInLabList = mrInLabsData.getMrInLabs().getMrInLabList();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return mrInLabList;
    }
    public static String mrInLabToXML(List<MrInLab> person) throws JAXBException, TransformerException {

        MrInLabsData mrInLabsData = new MrInLabsData();
        MrInLabs mrInLabs = new MrInLabs();
        mrInLabs.setMrInLabList(person);
        mrInLabsData.setMrInLabs(mrInLabs);

        JAXBContext jaxbContext = JAXBContext.newInstance(MrInLabsData.class);

        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // 输出格式化
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(mrInLabsData, sw);

        // 获取生成的XML字符串（包含XML声明头）
        String xmlWithDeclaration = sw.toString();

        // 使用Transformer来移除XML声明头
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        // 由于StringSource不是标准API的一部分，我们使用StreamSource并跳过XML声明
        // 我们通过Transformer直接输出到另一个StringWriter来避免XML声明
        Source inputSource = new javax.xml.transform.stream.StreamSource(new java.io.StringReader(xmlWithDeclaration));
        StringWriter outputStringWriter = new StringWriter();
        StreamResult outputStreamResult = new StreamResult(outputStringWriter);

        // 执行转换
        transformer.transform(inputSource, outputStreamResult);

        // 获取最终的XML字符串（不带XML声明头）
        String xmlWithoutDeclaration = outputStringWriter.toString();

        return xmlWithoutDeclaration;
    }

    // 你还需要一个MrInLabs类来包装MrInLab列表
//    @XmlRootElement(name = "mrInLabsData")
//    @XmlAccessorType(XmlAccessType.FIELD)
//    static class MrInLabs {
//        @XmlElement(name = "mrInLabs")
//        private MrInLabs.MrInLabsWrapper mrInLabsWrapper;
//
//        // 假设我们有一个包装类来处理<mrInLabs>元素
//        static class MrInLabsWrapper {
//            @XmlElement(name = "MrInLab")
//            private List<MrInLab> mrInLab;
//
//            public List<MrInLab> getMrInLab() {
//                return mrInLab;
//            }
//
//            public void setMrInLab(List<MrInLab> mrInLab) {
//                this.mrInLab = mrInLab;
//            }
//        }
//
//        public MrInLabs.MrInLabsWrapper getMrInLabsWrapper() {
//            return mrInLabsWrapper;
//        }
//
//        public void setMrInLabsWrapper(MrInLabsWrapper mrInLabsWrapper) {
//            this.mrInLabsWrapper = mrInLabsWrapper;
//        }
//    }
}
